//
//  ChannelVC.m
//  NewMyMusicAPP
//
//  Created by 11_wyq on 15/6/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ChannelVC.h"

@interface ChannelVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (retain ,nonatomic) MBProgressHUD *MBP;

@end

@implementation ChannelVC


- (void)dealloc
{
    
    [_ID release];
    [_link release];
    [_title_label release];
    [_array release];
    [_collection release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    //       给导航栏上放自定义label
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 100,HEIGHT * 30)]autorelease];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 35, 0,WIDTH * 50,HEIGHT * 30)]autorelease];
    title.text = @"频道";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    self.navigationController.navigationBar.translucent = NO;//取消NavBar隐藏
    
    UIImage *image2 = [UIImage imageNamed:@"menu.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(locker)];
    
    
    
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[[[UICollectionViewFlowLayout alloc] init]autorelease]autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTH * 185,HEIGHT * 185);
    flowLayout.minimumLineSpacing = WIDTH * 5;
    flowLayout.minimumInteritemSpacing = HEIGHT * 5;
    //flowLayout.headerReferenceSize = CGSizeMake(MYWIDTH,HEIGHT * 130);
    flowLayout.footerReferenceSize = CGSizeMake(MYWIDTH,HEIGHT * 64);
    self.collection = [[[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout]autorelease];
    self.collection.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    //注册一个cell
    [self.collection registerClass:[ChannelCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
//    self.img = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MYWIDTH,HEIGHT * 130)]autorelease];
//    self.img.backgroundColor = [UIColor blackColor];
//    [self.collection addSubview:self.img];
    
    self.title_label = [[[UILabel alloc] initWithFrame:CGRectMake(WIDTH * 10,HEIGHT * 110, MYWIDTH,HEIGHT * 20)]autorelease];
    self.title_label.textColor = [UIColor whiteColor];
    [self.title_label setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];//加粗字体
    [self.img addSubview:self.title_label];
 
    
  
    self.collection.showsVerticalScrollIndicator = NO;//取消纵向滑条
    
    //动态小黄人
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    [self asker];
}

- (void)myWebView
{
    VideoPlayerViewController *videoPlayer = [[[VideoPlayerViewController alloc] init]autorelease];
    videoPlayer.hidesBottomBarWhenPushed = YES;//隐藏下方Bar
    videoPlayer.url = self.link;
    videoPlayer.type = @"ACTIVITY";
    videoPlayer.Id = self.ID;
    [self.navigationController pushViewController:videoPlayer animated:YES];
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Channel_ViewController *channel = [[[Channel_ViewController alloc] init]autorelease];
    if (self.array.count != 0) {
        ChannelModel *channelModel = [self.array objectAtIndex:indexPath.row];
        channel.Id = channelModel.Id;
        channel.title = channelModel.title;
    }
    
    [self.navigationController pushViewController:channel animated:YES];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;
    }else
    {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    
    if (self.array.count != 0) {
        cell.channelModel = [self.array objectAtIndex:indexPath.row];
    }
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    return  cell;
    
}

- (void)asker
{
    [WebRequest connectWithUrl:ChannelPage parmater:nil requestHeader:RequestHeader httpMethod:@"GET" view:self.view block:^(id data) {
        

        self.array = [NSMutableArray array];
        NSMutableDictionary *dicData = data;
        
        
        //下面瀑布流数据
        NSMutableArray *array = [dicData objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            ChannelModel *channelModel = [[[ChannelModel alloc] init]autorelease];
            channelModel.Id = [dic objectForKey:@"id"];
            channelModel.img = [dic objectForKey:@"img"];
            channelModel.title = [dic objectForKey:@"title"];
            
            [self.array addObject:channelModel];
        }
        
       [GiFHUD dismiss];//动态图隐藏
        [self.collection reloadData];
        
    } refresh:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            //[self asker]; //重新请求
           [GiFHUD dismiss];//动态图隐藏 

        });
    }];
}


//抽屉
-(void)locker
{
    [GiFHUD dismiss];//动态图隐藏 
    [self.sideMenuViewController presentLeftMenuViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
